﻿using DeepZoomPivotConstructor.PivotLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using DeepZoomPivotConstructor.VisualLib;
using System.Linq;
using System.Drawing;

namespace t_PivotLib
{

    /// <summary>
    ///This is a test class for PivotImageTest and is intended
    ///to contain all PivotImageTest Unit Tests
    ///</summary>
    [TestClass()]
    public class t_PivotImage
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        /// <summary>
        ///A test for PivotImage Constructor
        ///</summary>
        [TestMethod()]
        public void PivotImageConstructorTest()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v);
            Assert.AreEqual(v, target.Image, "The image visual was not set correctly!");
        }

        [TestMethod]
        public void TestBasicAccessors()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v);

            target.Name = "NameMe";
            Assert.AreEqual("NameMe", target.Name, "Name isn't set");

            target.Description = "This is a test";
            Assert.AreEqual("This is a test", target.Description, "Description didn't make it!");
        }

        [TestMethod]
        public void TestStringFacet()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v) { { "F1", "value1" }, { "F2", "value2" } };
            Assert.AreEqual(2, target.Count(), "Wrong number of added meta data");
        }

        [TestMethod]
        public void TestURLFacet()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v) { { "F1", "nytimes", new Uri("http://www.nytimes.com") }, { "F2", "CNN", new Uri("http://www.cnn.com") } };
            Assert.AreEqual(2, target.Count(), "Wrong number of added meta data");
        }

        [TestMethod]
        public void TestNumberFacet()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v) { { "F1", 5 }, { "F2", (decimal) 55.5 } };
            Assert.AreEqual(2, target.Count(), "Wrong number of added meta data");
        }

        [TestMethod]
        public void TestDatTimeFacet()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v) { { "F1", DateTime.Now }, { "F2", DateTime.Now } };
            Assert.AreEqual(2, target.Count(), "Wrong number of added meta data");
        }

        [TestMethod]
        public void TestFetchMetadata()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v) { { "F1", DateTime.Now }, { "F2", DateTime.Now }, { "F4", "CNN", new Uri("http://www.cnn.com") } };

            var result = target.GetMetadata<PivotMetadataDateTime>("F1");
            Assert.AreEqual(1, result.Count(), "Expected to get back my meta-data!");

            var result2 = target.GetMetadata<PivotMetadataURL>("F4");
            Assert.AreEqual(1, result2.Count(), "Expected to get back the URL meta data I wanted!");

        }

        [TestMethod]
        public void TestAddingSameStringMetaData()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v) { { "F1", "hi" }, { "F1", "hi" }, { "F1", "CNN" } };

            var result = target.GetMetadata<PivotMetadataString>("F1");
            Assert.AreEqual(2, result.Count(), "Incorrect number of F1 meta-data's");
        }

        [TestMethod]
        public void TestMetadataClash()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v) { { "F1", "hi" }, { "F1", "CNN" } };
            target.Add("F1", "files", new Uri("Http://www.nytimes.com"));

            Assert.AreEqual(1, target.GetMetadata<PivotMetadataURL>("F1").Count(), "Unexpected number of URL's came back!");
            Assert.AreEqual(2, target.GetMetadata<PivotMetadataString>("F1").Count(), "Unexpected # of strings came back!");
        }

        [TestMethod]
        public void TestClearAddedMetadata()
        {
            PivotImage target = new PivotImage(null) { {"F1", "F1"}};
            Assert.AreEqual(1, target.GetMetadata<PivotMetadataString>("F1").Count(), "Expected the string F1 to be in there!");
            target.Remove("F1");
            Assert.AreEqual(0, target.GetMetadata<PivotMetadataString>("F1").Count(), "Expected the string F1 to have been deleted!");

            target.Add("F1", "F2");
            target.Add("F1", "F3");
            target.Add("F1", "files", new Uri("http://www.nytimes.com"));

            target.Remove("F1");
            Assert.AreEqual(0, target.GetMetadata<PivotMetadataString>("F1").Count(), "Expected the string F1 to have been deleted even with multiple items!");
        }

        [TestMethod]
        public void TestRemoveSpecificMetadata()
        {
            PivotImage target = new PivotImage(null);
            target.Add("dude", "fork", new Uri("http://www.null.com"));
            target.Add("dude", "spoon", new Uri("http://www.spoon.com"));

            target.Remove<PivotMetadataURL>("dude", pmurl => pmurl.LinkText == "spoon");
            Assert.AreEqual(1, target.GetMetadata<PivotMetadataURL>("dude").Count(), "Expected only a single dude url left!");
            Assert.AreEqual("fork", target.GetMetadata<PivotMetadataURL>("dude").FirstOrDefault().LinkText, "Did the wrong one get removed?");
        }

        [TestMethod]
        public void TestClearBogusInput()
        {
            PivotImage target = new PivotImage(null) { { "F1", "F1" } };
            target.Remove("");
            target.Remove("DORK");
            Assert.AreEqual(1, target.GetMetadata<PivotMetadataString>("F1").Count(), "Expected the string F1 to be in there!");
        }

        [TestMethod]
        public void TestAddOthersAndThenString()
        {
            Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
            PivotImage target = new PivotImage(v);
            target.Add("F1", "files", new Uri("Http://www.nytimes.com"));

            target.Add("Dude", "hi there");

            Assert.AreEqual(1, target.GetMetadata<PivotMetadataString>("Dude").Count(), "Unexpected numbrer of strings stored!");
        }

    }
}